//Connector New Dataset
clear all
global data "/disk/eagedisk1/mahc_sl/data/stata/20210121/"
global eligibility "/disk/eagedisk1/mahc_sl.work/Master_Formatted_Data/JPAL_Flatfiles/JPAL_Demographics_082019"
global raw_elig "../../Master_Formatted_Data/Eligibility/raw_eligibility_20210922_jpalonly"
global zip "/homes/nber/mcintyrea/mahc_sl.work/Master_Formatted_Data/zip2stablewrap.dta"

adopath + ../ado/

program main
	clean_Dec_file
	enrollment
	balance
	regression_prep
	member_months_sample
	persistence_setup
	rd_setup
end

program clean_Dec_file
use ${data}Mark_AsOfDate12_02_2020, clear
drop if PeriodId>202012 |PeriodId<202001
save ../output/Enrollment_2020, replace
end

program enrollment
use ${data}Mark_AsOfDate07_02_2020, clear
drop if PeriodId>201912
append using ../output/Enrollment_2020, force

drop if (Carrier=="ALTUS" | Carrier=="DELTADENTAL" | Carrier=="Delta Dental" | Carrier == "Altus Dental")
drop if UniqueId==""
bysort UniqueId PeriodId: gen dups_id=_n	
bysort UniqueId PeriodId: egen dups=max(dups_id)
	replace dups=0 if dups==1
	replace dups=1 if dups>1
gen assigned_arm = 0
replace assigned_arm = 1 if (Arm_Number=="1" | Arm_Number=="2"| Arm_Number=="3" | Arm_Number=="4")
by UniqueId: egen ever_assigned = mean(assigned_arm) 
replace ever_assigned = 1 if ever_assigned > 0 & ever_assigned !=.

gen report_mo = substr(ReportDate,6,2)
gen report_yr = substr(ReportDate,1,4)
replace report_yr = "" if report_yr=="N/A"
gen report_ym = report_yr+report_mo
destring(report_ym), replace
destring(report_yr), replace
gen r_y2 = report_yr-2
tostring r_y2, replace
gen report_m2 = r_y2+report_mo
drop report_mo report_yr r_y2
destring report_m2, replace

gen before_intervention = 0 
replace before_intervention = 1 if PeriodId < report_ym & PeriodId>=report_m2 & !mi(report_ym) & ever_assigned==1

by UniqueId: egen prior_enroll = mean(before_intervention) 
replace prior_enroll = 1 if prior_enroll > 0 & prior_enroll!=.
local date_vars ReportDate EffectiveDate BenefitStartDate TermDate
	foreach var of local date_vars{	
	rename `var' `var'_date
	gen `var' = date(`var'_date, "YMD")
	format `var' %td
	drop `var'_date
	}
tostring PeriodId, gen(PeriodId_string)

	gen int PeriodId_year = real(substr(PeriodId_string,1,4))
	gen int PeriodId_month = real(substr(PeriodId_string,5,2))

	gen PeriodId_date = ym(PeriodId_year,PeriodId_month)
	format PeriodId_date %tmMCY
	
	
 
	generate before_intervention_2 = 0 
	replace before_intervention_2 = 1 if ReportDate == (mdy(7,3,2018)) & PeriodId < 201807 & ever_assigned==1 &  PeriodId>=201607
	replace before_intervention_2 = 1 if ReportDate == (mdy(7,17,2018)) & PeriodId < 201807 & ever_assigned==1 & PeriodId>=201607
	replace before_intervention_2 = 1 if ReportDate == (mdy(7,31,2018)) & PeriodId < 201807 & ever_assigned==1 & PeriodId>=201607
	replace before_intervention_2 = 1 if ReportDate == (mdy(8,14,2018)) & PeriodId < 201808 & ever_assigned==1 & PeriodId>=201608
	replace before_intervention_2 = 1 if ReportDate == (mdy(8,28,2018)) & PeriodId < 201808 & ever_assigned==1 & PeriodId>=201608
	replace before_intervention_2 = 1 if ReportDate == (mdy(9,11,2018)) & PeriodId < 201809 & ever_assigned==1 & PeriodId>=201609
	replace before_intervention_2 = 1 if ReportDate == (mdy(3,19,2019)) & PeriodId < 201903 & ever_assigned==1 & PeriodId>=201703
	replace before_intervention_2 = 1 if ReportDate == (mdy(4,2,2019)) & PeriodId < 201904 & ever_assigned==1 & PeriodId>=201704
 
	replace before_intervention_2 = 1 if ReportDate == (mdy(4,16,2019)) & PeriodId < 201904 & ever_assigned==1 & PeriodId>=201704
	replace before_intervention_2 = 1 if ReportDate == (mdy(4,30,2019)) & PeriodId < 201904 & ever_assigned==1 & PeriodId>= 201704
	replace before_intervention_2 = 1 if ReportDate == (mdy(5,14,2019)) & PeriodId < 201905 & ever_assigned==1 & PeriodId>=201705
	replace before_intervention_2 = 1 if ReportDate == (mdy(5,28,2019)) & PeriodId < 201905 & ever_assigned==1 & PeriodId>= 201705
	replace before_intervention_2 = 1 if ReportDate == (mdy(6,11,2019)) & PeriodId < 201906 & ever_assigned==1 & PeriodId>=201706
	replace before_intervention_2 = 1 if ReportDate == (mdy(6,25,2019)) & PeriodId < 201906 & ever_assigned==1 & PeriodId>= 201706
	
	by UniqueId: egen prior_enroll_2 = mean(before_intervention_2) 
	replace prior_enroll_2 = 1 if prior_enroll_2 > 0 & prior_enroll_2!=.
	generate letter_mailed = . 
	replace letter_mailed = mdy(7,10,2018) if ReportDate == (mdy(7,3,2018))
	replace letter_mailed = mdy(7,31,2018) if ReportDate == (mdy(7,17,2018))
	replace letter_mailed = mdy(8,7,2018) if ReportDate == (mdy(7,31,2018))
	replace letter_mailed = mdy(8,22,2018) if ReportDate == (mdy(8,14,2018))
	replace letter_mailed = mdy(9,5,2018) if ReportDate == (mdy(8,28,2018))
	replace letter_mailed = mdy(9,18,2018) if ReportDate == (mdy(9,11,2018))
	replace letter_mailed = mdy(3,25,2019) if ReportDate == (mdy(3,19,2019))
	replace letter_mailed = mdy(4,8,2019) if ReportDate == (mdy(4,2,2019))
 
	replace letter_mailed = mdy(4,22,2019) if ReportDate == (mdy(4,16,2019))
	replace letter_mailed = mdy(5,6,2019) if ReportDate == (mdy(4,30,2019))
	replace letter_mailed = mdy(5,20,2019) if ReportDate == (mdy(5,14,2019))
	replace letter_mailed = mdy(6,3,2019) if ReportDate == (mdy(5,28,2019))
	replace letter_mailed = mdy(6,17,2019) if ReportDate == (mdy(6,11,2019))
	replace letter_mailed = mdy(7,1,2019) if ReportDate == (mdy(6,25,2019))
	
	
	format letter_mailed %td
	
	gen enrollafterletter=0
	replace enrollafterletter=1 if EffectiveDate > letter_mailed & EffectiveDate!=. & letter_mailed!=.
	gen enrollby90 = 0
	replace enrollby90 = 1 if ((EffectiveDate - letter_mailed) <= 90) 
	replace enrollby90 = 0 if ((EffectiveDate - letter_mailed) <= 0) 
	by UniqueId: egen enroll90 = mean(enrollby90) 
	replace enroll90 = 1 if enroll90 > 0 & enroll90 !=.
	
	
	gen time_to_enrollment = EffectiveDate - letter_mailed
	gen plantype1 = (PlanType == "1")
	gen plantype2A = (PlanType == "2A" | PlanType == "AI-2A")
	gen plantype2B = (PlanType == "2B" | PlanType == "AI-2B")
	gen plantype3A = (PlanType == "3A" | PlanType == "AI-3A")
	gen plantype3B = (PlanType == "3B" | PlanType == "AI-3B")
gen zeropremium_enrolled = (plantype1==1 & FamilyLevelBalancePremiumAmt == 0 | plantype2A==1 & FamilyLevelBalancePremiumAmt == 0)
recast str36 UniqueId, force

//Merge to old eligibility file
merge m:1 UniqueId letter_mailed using ${eligibility}
drop if jpal_duplicate == 1 
drop if dups_id == 2 
replace enroll90 = 0 if _merge == 2
replace prior_enroll = 0 if _merge == 2
replace prior_enroll_2 = 0 if _merge == 2
sort UniqueId PeriodId
by UniqueId: gen id = _n 
by UniqueId: egen max_id = max(id) 
gen most_recent_obs = (id == max_id) 



sort UniqueId enrollby90 PeriodId
by UniqueId enrollby90: egen use_id_90 = min(PeriodId)
replace use_id_90=1 if use_id_90==PeriodId & enrollby90==1
replace use_id_90 = 0 if use_id_90 != 1
replace use_id_90 = most_recent_obs if enroll90==0 & most_recent_obs==1
replace use_id_90 = 1 if _merge == 2
forval i = 1/4{ 
gen arm`i' = elig_armnumber == `i'	
	}
rename elig_arm arm
gen zeropremium_eligible = 0
replace zeropremium_eligible = 1 if elig_fpl <= 150
gen female=0
replace female=1 if elig_gender=="F"

replace zeropremium_eligible = 1 if elig_fpl <= 150
replace elig_fpl = (elig_fpl*-1) if elig_fpl <0
gen incomebracket = 0
replace incomebracket = 1 if (elig_fpl <=138)
replace incomebracket = 2 if (elig_fpl > 138 & elig_fpl <=150)
replace incomebracket = 3 if (elig_fpl > 150 & elig_fpl <=200)
replace incomebracket = 4 if (elig_fpl > 200 & elig_fpl <=250)
replace incomebracket = 5 if (elig_fpl > 250 & elig_fpl <=300)
gen zeropremium_enrolled_fixed = (enroll90 == 1 & (plantype1==1 | plantype2A==1) & FamilyLevelBalancePremiumAmt==0)
gen jpal_treated = . 
replace jpal_treated = 0 if arm==1
replace jpal_treated = 1 if arm > 1 & arm!=.

gen log_balancepremium = log(FamilyLevelBalancePremiumAmt+1)
gen age_category = . 
replace age_category = 1 if elig_age <= 18
replace age_category = 2 if elig_age >= 19 & elig_age <= 24
replace age_category = 3 if elig_age >= 25 & elig_age <= 29
replace age_category = 4 if elig_age >= 30 & elig_age <= 34
replace age_category = 5 if elig_age >= 35 & elig_age <= 39
replace age_category = 6 if elig_age >= 40 & elig_age <= 44
replace age_category = 7 if elig_age >= 45 & elig_age <= 49
replace age_category = 8 if elig_age >= 50 & elig_age <= 54
replace age_category = 9 if elig_age >= 55 & elig_age <= 59
replace age_category = 10 if elig_age >= 60 & elig_age!=.
destring elig_zip, replace
tostring elig_zip, gen(zip_string)

gen zip3digit = substr(zip_string,-4,2) if elig_zip < 10000 & elig_zip!=. /* -4 is 4 from the right side for 2 characters */

* For zip codes not starting with zero 
replace zip3digit = substr(zip_string,-5,3) if elig_zip > 10000 & elig_zip!=. /* -5 is 5 from the right side for 3 characters */

* For elig_zip = 802, which we assume is 00802 (St. Thomas, Virigin Islands)
replace zip3digit="8" if elig_zip==802


destring zip3digit, replace

	gen batch = .
	replace batch = 1 if letter_mailed == mdy(7,10,2018)
	replace batch = 2 if letter_mailed == mdy(7,31,2018)
	replace batch = 3 if letter_mailed == mdy(8,7,2018)
	replace batch = 4 if letter_mailed == mdy(8,22,2018)
	replace batch = 5 if letter_mailed == mdy(9,5,2018)
	replace batch = 6 if letter_mailed == mdy(9,18,2018)
	replace batch = 7 if letter_mailed == mdy(3,25,2019)
	replace batch = 8 if letter_mailed == mdy(4,8,2019)
	replace batch = 9 if letter_mailed == mdy(4,22,2019)
	replace batch = 10 if letter_mailed == mdy(5,6,2019)
	replace batch = 11 if letter_mailed == mdy(5,20,2019)
	replace batch = 12 if letter_mailed == mdy(6,3,2019)
	replace batch = 13 if letter_mailed == mdy(6,17,2019)
	replace batch = 14 if letter_mailed == mdy(7,1,2019) 

	gen time_to_formdue = (letter_formdue - letter_mailed)
	gen time_to_paydue = (letter_paydue - letter_mailed)
	
	   gen PlanYearId = .
	   replace PlanYearId = 2018 if letter_mailed < mdy(12,31,2018)
	   replace PlanYearId = 2019 if letter_mailed > mdy(12,31,2018)

		label var arm2 "Arm 2: Generic reminder"
		label var arm3 "Arm 3: Personalized reminder"
		label var arm4 "Arm 4: Streamlined enrollment"
		label var elig_fpl "Income (FPL)" 
		label var female "Female"
		label var spanish_letter "Received Spanish letter"
		label var prior_enroll_2 "Prior Connector enrollment"
		
		
 
		gen boston = 0
		
		forval i = 2108/2136{
			replace boston = 1 if (elig_zip ==`i')
			}
		
		foreach i in 22163 2199 2203 2210 2215 2222 ///
		2112 2117 2123 2137 2196 2205 2283 2284 2298 2201 2204 2206  ///
		2211 2212 2217 2241 2266 2293 2297{
			replace boston = 1 if (elig_zip ==`i')
			}			

gen time_elapsed = (letter_mailed - elig_DeterminationDate)
gen zipstring  = string(elig_zip,"%05.0f")
drop zip_string
gen ZipCode = zipstring

   gen notMAzip = elig_zip > 2791
   replace notMAzip=0 if elig_zip==.
rename _merge _merge_eligtoenroll

merge m:1 ZipCode PlanYearId using $zip
  
replace notMAzip = 1 if zipstring=="00802"

replace WrapRegion = "A2" if (zipstring=="01014" | zipstring=="01041" | zipstring=="01102" | zipstring=="01116")

replace WrapRegion = "A3" if zipstring=="01227"

replace WrapRegion = "C2" if zipstring == "01704"

replace WrapRegion = "D1" if (zipstring== "01866" | zipstring == "01937")

replace WrapRegion = "C1" if (zipstring == "02027" | zipstring == "02040")

replace WrapRegion = "E1" if (zipstring == "02196" | zipstring == "02447" | zipstring == "02112" | zipstring == "02455")
replace WrapRegion = "G2" if zipstring == "02552"
drop if notMAzip==1
encode WrapRegion, gen(WrapRegion_f)
replace Gender="M" if Gender=="Male"
replace Gender="F" if Gender=="Female"

save ../output/full_population, replace

 
drop if assigned_arm==0
drop if letter_mailed == mdy(7,1,2019)
drop if mi(UniqueId)

merge m:1 UniqueId using ../output/adj_fpl, keep(1 3) nogen keepusing(new_fpl)
gen new_elig = 0
replace new_elig = 1 if new_fpl<=150

label var new_fpl "Income (FPL)" 
label var new_elig "Zero Premium Eligibility"

save ../output/jpal_sample, replace
end

program balance
*Add Enrollment Tag
use ../output/jpal_sample, clear
keep UniqueId PeriodId
duplicates drop
drop if missing(UniqueId)
recast str36 UniqueId
gen enroll_tag = 1
save ../output/enrollment_tag, replace

use ${raw_elig}, clear
rename PeriodId PeriodId_string
gen PeriodId=date(PeriodId_string, "YM")
format PeriodId %td
merge m:1 UniqueId PeriodId using ../output/enrollment_tag, keep(1 3)
replace enroll_tag = 0 if missing(enroll_tag)

*Add Average FPL
bys UniqueId: egen n_fpl = mean(qhpfpl2) if time_to_reportdate>-30 & time_to_reportdate<=0
bys UniqueId: egen new_fpl = max(n_fpl)
drop n_fpl
preserve
keep UniqueId new_fpl
duplicates drop
save ../output/adj_fpl, replace
restore
save ../output/jpal_enrollment_file, replace
end

program regression_prep
use ../output/jpal_sample, clear

drop if assigned_arm == 0 

gen wave1 = (use_id_90==1 & letter_mailed < (mdy(1,1,2019)))
gen wave2 = (use_id_90==1 & letter_mailed > (mdy(1,1,2019)))
gen wave = .
replace wave=1 if wave1==1
replace wave=2 if wave2==1

gen enrollment_days = 0
replace enrollment_days = TermDate-EffectiveDate if (TermDate!=. & EffectiveDate!=.)
 
gen enroll_90_plus = 0
replace enroll_90_plus = 1 if enrollment_days>=90
gen enroll_year_plus = 0
replace enroll_year_plus = 1 if enrollment_days >=365

tabstat enrollment_days enroll_90_plus enroll_year_plus, stats(mean) columns(stats)
save ../output/connector_regression_prep, replace
end

program member_months_sample
use ../output/jpal_sample, clear

replace PeriodId = 999999 if _merge_eligtoenroll==2
drop if assigned_arm==0

drop if letter_mailed==mdy(7,1,2019)
drop if mi(UniqueId)

   gen PeriodId_endofmonth = .
   
   forval i = 2018/2020{
   
   replace PeriodId_endofmonth = mdy(1,31,`i') if PeriodId==`i'01
   replace PeriodId_endofmonth = mdy(1,28,`i') if PeriodId==`i'02
   replace PeriodId_endofmonth = mdy(3,31,`i') if PeriodId==`i'03
   replace PeriodId_endofmonth = mdy(4,30,`i') if PeriodId==`i'04
   replace PeriodId_endofmonth = mdy(5,31,`i') if PeriodId==`i'05
   replace PeriodId_endofmonth = mdy(6,30,`i') if PeriodId==`i'06
   replace PeriodId_endofmonth = mdy(7,31,`i') if PeriodId==`i'07
   replace PeriodId_endofmonth = mdy(8,31,`i') if PeriodId==`i'08
   replace PeriodId_endofmonth = mdy(9,30,`i') if PeriodId==`i'09
   replace PeriodId_endofmonth = mdy(10,31,`i') if PeriodId==`i'10
   replace PeriodId_endofmonth = mdy(11,30,`i') if PeriodId==`i'11
   replace PeriodId_endofmonth = mdy(12,31,`i') if PeriodId==`i'12
}

gen PeriodId_dt = mdy(PeriodId_month,1,PeriodId_year)
format PeriodId_dt %td

* Testing if using id_enr_days instead of use_id_90 works
gen enr_by_X_days = 0
replace enr_by_X_days = 1 if ((EffectiveDate - letter_mailed) <= 90) 

replace enr_by_X_days = 0 if ((EffectiveDate - letter_mailed) <= 0) 
sort UniqueId
by UniqueId: egen enr_days = mean(enr_by_X_days) 
replace enr_days = 1 if enr_days > 0 & enr_days !=.

sort UniqueId enr_by_X_days PeriodId
by UniqueId enr_by_X_days: egen id_enr_days = min(PeriodId)
replace id_enr_days=1 if id_enr_days==PeriodId & enr_by_X_days==1
replace id_enr_days = 0 if id_enr_days != 1

replace id_enr_days = most_recent_obs if enroll90==0 & most_recent_obs==1

*For people who only appear in the elig file 
replace id_enr_days = 1 if _merge_eligtoenroll == 2
		
egen long UniqueId_num = group(UniqueId)
bysort UniqueId_num PeriodId_date (EffectiveDate): gen num = _n
drop if num>1
drop num

tsset UniqueId_num PeriodId_date
cap ssc install tsspell
  
tsspell, fcond(PeriodId_date != PeriodId_date[_n-1] + 1  | (_n == 1))

sort UniqueId_num _spell
by UniqueId_num _spell: egen spell_maxrun = max(_seq)
gen study_spell_temp = _spell if use_id_90 == 1
bys UniqueId_num: egen study_spell = max(study_spell_temp)

gen temp_timetoenrollment = time_to_enrollment if id_enr_days==1
sort UniqueId
by UniqueId: egen use_timetoenrollment = min(temp_timetoenrollment)

sort UniqueId PeriodId
by UniqueId: egen max_PeriodId_endofmonth = max(PeriodId_endofmonth)

save ../output/member_months_sample, replace

gen wave1 = (use_id_90==1 & letter_mailed < (mdy(1,1,2019)))
gen wave2 = (use_id_90==1 & letter_mailed > (mdy(1,1,2019)))
gen wave = .
replace wave=1 if wave1==1
replace wave=2 if wave2==1

gen enrollment_days = 0
replace enrollment_days = TermDate-EffectiveDate if (TermDate!=. & EffectiveDate!=.)

gen enroll_90_plus = 0
replace enroll_90_plus = 1 if enrollment_days>=90
gen enroll_year_plus = 0
replace enroll_year_plus = 1 if enrollment_days >=365

tabstat enrollment_days enroll_90_plus enroll_year_plus, stats(mean) columns(stats)
drop if mi(UniqueId)
save ../output/mm_regression_prep, replace
end

program persistence_setup
clear all
use ../output/member_months_sample

gen useid_PeriodId_temp = PeriodId if use_id_90 == 1
bys UniqueId: egen useid_PeriodId = max(useid_PeriodId_temp)
drop if PeriodId < (useid_PeriodId-4) & PeriodId !=.  
   
  replace PeriodId_date = ym(2020,12) if PeriodId==999999

  tsset UniqueId_num PeriodId_date 
  tsfill, full
  gen enrolled_this_month = 1 
  
  replace enrolled_this_month = 0 if UniqueId == ""
  replace enrolled_this_month = 0 if PeriodId == 999999
  
bys UniqueId_num: egen lettermailed_new = max(letter_mailed)
  
gen lettermailed_month = month(lettermailed_new)
gen lettermailed_year = year(lettermailed_new)
      
replace PeriodId_month = month(dofm(PeriodId_date))
replace PeriodId_year = year(dofm(PeriodId_date))
   
   
gen persistence_time = . 

* t = 0 is month when the letter was mailed 
replace persistence_time = 0 if lettermailed_month == PeriodId_month & ///
lettermailed_year == PeriodId_year

* Count forward from 0 to at least 24
sort UniqueId_num PeriodId_date 
bys UniqueId_num: replace persistence_time = persistence_time[_n-1]+1 if persistence_time[_n-1] != .
bys UniqueId_num: replace persistence_time = -1 if persistence_time[_n+1] == 0


* How many months enrolled on average?
bys UniqueId_num: egen total_months_enrolled = sum(enrolled_this_month)
mean(total_months_enrolled) if use_id_90 == 1 & enroll90 == 1 // 14! 

forvalues i = -1/24{
	
	local k = `i'+1
	
	gen enr_`k'months_postletter_temp = 0
	replace enr_`k'months_postletter_temp = 1 if persistence_time == `i' ///
	& enrolled_this_month == 1 
	bys UniqueId_num: egen enr_`k'months_postletter = max(enr_`k'months_postletter_temp)
	drop enr_`k'months_postletter_temp
	
}

sort UniqueId_num use_id_90
bys UniqueId_num: carryforward UniqueId, replace

foreach var in "arm" "arm1" "arm2" "arm3" "arm4" "batch" "new_elig" {
	bys UniqueId_num: egen `var'_temp = max(`var')
	replace `var' = `var'_temp if mi(`var')
	drop `var'_temp
}



forv zp = 0/1 {
	forv i = -1/12 {
		local k = `i'+1
	reg enr_`k'months_postletter i.arm i.batch if use_id_90==1 & new_elig==`zp', r
	reg enrolled_this_month i.arm i.batch if persistence_time == `i' & new_elig==`zp', r
	}
}
save ../output/persistence_months, replace	
end

program rd_setup
use ../output/full_population, clear
duplicates drop
drop if missing(UniqueId)

egen long UniqueId_num = group(UniqueId)
bys UniqueId_num PeriodId_date (EffectiveDate): gen num = _n
drop if num>1
drop num
replace PeriodId = 999999 if _merge==2

tsset UniqueId_num PeriodId_date
tsspell, fcond(PeriodId_date!=PeriodId_date[_n-1]+1|(_n==1))

sort UniqueId_num PeriodId_date
gen initiate_coverage = 0
replace initiate_coverage = 1 if _seq==1
replace initiate_coverage = 0 if (initiate_coverage==1) & (UniqueId[_n-1]==UniqueId) & (PeriodId[_n-1]==PeriodId-1)
replace initiate_coverage = 0 if (initiate_coverage==1) & (UniqueId[_n-1]==UniqueId) & (PeriodId[_n-1]==PeriodId-2)
replace initiate_coverage = 0 if (initiate_coverage==1) & (UniqueId[_n-1]==UniqueId) & (PeriodId[_n-1]==PeriodId-3)

keep if initiate_coverage==1

keep if PeriodId>= 201601 & PeriodId<202001
drop if AgeAt < 18
drop if AgeAt > 64

drop if FamilySize > 1
drop if IsSubscriber == 0

keep if (PlanType_CCA == "2A" | PlanType_CCA == "2B" | PlanType_CCA == "3A" | PlanType_CCA == "3B"|PlanType_CCA=="4")

drop if PlanType_CCA=="1"
drop if fpl<138 |fpl>=400
keep if !mi(fpl)

* Remove mismatching
gen correct_pt = 0
replace correct_pt = 1 if (PlanType_CCA=="2A" & fpl>100 & fpl<=150 & !mi(fpl))
replace correct_pt = 1 if (PlanType_CCA=="2B" & fpl>150 & fpl<=200 & !mi(fpl))
replace correct_pt = 1 if (PlanType_CCA=="3A" & fpl>200 & fpl<=250 & !mi(fpl))
replace correct_pt = 1 if (PlanType_CCA=="3B" & fpl>250 & fpl<=300 & !mi(fpl))
replace correct_pt = 1 if (PlanType_CCA=="4" & fpl>300 & fpl<=400 & !mi(fpl))
replace correct_pt = 1 if (PlanType_CCA=="5" & fpl>400 & !mi(fpl))

keep if correct_pt==1
keep if PeriodId_year>=2016 & PeriodId_year<=2019

sort UniqueId PeriodId_year PeriodId
bys UniqueId PeriodId_year (PeriodId): gen timeline = _n
keep if timeline==1
gen n = 1
replace n = . if mi(fpl)

* 1,2,5% FPL bins
foreach i of numlist 1 2 5 {
	if `i'== 2 local min = 136
	if `i'!=2 local min = 135
	gen bin_`i' = .
	forv x = `min'(`i')400 {
	local y = `x'+`i'
	replace bin_`i'=`x' if (fpl>=`x'&fpl<`y'&!mi(fpl))
	}
}

save ../output/rd_setup_first_enroll,replace
end

 * Execute
 main
